# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=shfmt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6.0
pkgrel=1
pkgdesc='Format shell programs (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/mvdan/sh'
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-scdoc")
options=('!strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/mvdan/sh/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('4fe5a7b0aab5fba437696b68c5424e49ba9f793de2f534647e1d814108ebad23')

build() {
    cd "${srcdir}/sh-${pkgver}/cmd/shfmt/"
    export GOOS=windows
    export GOROOT=${MINGW_PREFIX}/lib/go
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      i686|x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    go build
    scdoc < "${_realname}.1.scd" | gzip > "${_realname}.1.gz"
}

check(){
    cd "${srcdir}/sh-${pkgver}/"
    go test ./... || warning "Tests failed"
}

package() {
    cd "${srcdir}/sh-${pkgver}/"
    install -Dm755 -t "${pkgdir}${MINGW_PREFIX}/bin/" "cmd/shfmt/shfmt.exe"
    install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1/" "cmd/shfmt/shfmt.1.gz"
    install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/shfmt/" LICENSE
}
